package com.ddpt.permission.service.impl;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.lh.center.common.base.BizServiceImpl;
import com.lh.center.permission.api.bo.BackUserSessionInfo;
import com.lh.center.permission.api.entity.BackUserEntity;
import com.lh.center.permission.mapper.BackUserMapper;
import com.lh.center.permission.service.BackUserService;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.rpc.RpcException;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
public class BackUserServiceImpl extends BizServiceImpl<BackUserMapper, BackUserEntity> implements BackUserService {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    //总后台用户token前缀
    private static final String userTokenKeyPrefix = "back_user_token_";

    //总后台图片验证码前缀
    private static final String imgCodeKeyPrefix = "back_user_imgCode_";

    @Override
    public BackUserSessionInfo loginByPassword(String account, String password, String imgCode, String codeId) {
        if(StringUtils.isBlank(account)
                || StringUtils.isBlank(password)
                || StringUtils.isBlank(imgCode)
                || StringUtils.isBlank(codeId)
                || !codeId.startsWith(imgCodeKeyPrefix)){
            return null;
        }
        String originCode = stringRedisTemplate.opsForValue().get(codeId);
        if(StringUtils.isBlank(originCode)){
            throw new RpcException("验证码已失效");
        }
        if(!originCode.equals(imgCode)){
            throw new RpcException("验证码不正确");
        }
        BackUserEntity user = getOne(Wrappers.lambdaQuery(BackUserEntity.class).eq(BackUserEntity::getAccount, account),false);
        if(user==null || !user.getPassword().equals(SecureUtil.md5(password))){
            throw new RpcException("用户名或密码错误");
        }
        return replenish(user);
    }

    @Override
    public BackUserSessionInfo loginByPhone(String phone, String code) {
        return null;
    }

    @Override
    public BackUserSessionInfo getInfo(String token) {
        if(StringUtils.isBlank(token) || !token.startsWith(userTokenKeyPrefix)){
            return null;
        }
        String val = stringRedisTemplate.opsForValue().get(token);
        if(StringUtils.isBlank(val)){
            return null;
        }
        return JSONUtil.toBean(val,BackUserSessionInfo.class);
    }

    @Override
    public void refresh(String token) {
        stringRedisTemplate.expire(token,30, TimeUnit.MINUTES);
    }

    /**
     * 补充其他信息
     * @param user  后台用户
     */
    private BackUserSessionInfo replenish(BackUserEntity user){
        BackUserSessionInfo info = new BackUserSessionInfo();
        info.setUserId(user.getId());
        info.setInfo(user);
        info.setToken(userTokenKeyPrefix+UUID.randomUUID().toString().replace("-",""));
        info.setSecret(RandomUtil.randomString(16));
        info.setAccount(user.getAccount());
        return info;
    }
}
